package com.cat.DataStructure04;

import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/most-frequent-ids/
 * @create 2025/11/26 19:15
 * @since JDK17
 */

public class Solution19 {
    public long[] mostFrequentIDs(int[] nums, int[] freq) {
        Map<Integer, Long> map = new HashMap<>();
        PriorityQueue<long[]> heap = new PriorityQueue<>((a, b) -> Long.compare(b[1], a[1]));
        int n = nums.length;
        long[] ans = new long[n];
        for (int i = 0; i < n; i++) {
            map.merge(nums[i], (long) freq[i], Long::sum); // 更改哈希表中的值
            heap.add(new long[] { nums[i], map.get(nums[i]) });
            while (!heap.isEmpty()) {
                var x = heap.peek();
                if (map.get((int) x[0]) == x[1]) {
                    break;
                }
                heap.poll();
            }
            if (!heap.isEmpty()) {
                ans[i] = heap.peek()[1];
            }
        }

        return ans;
    }
}
